home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / GENCTXT.ZIP / CHAP8.TXT < prev    next >
Text File  |  1987-11-21  |  19KB  |  455 lines

  1.  
  2.                             Chapter 8 - Pointers
  3.  
  4.  
  5.                              WHAT IS A POINTER?
  6.  
  7.              Simply  stated,  a pointer is an address.   Instead  of
  8.         being  a  variable,  it  is a pointer to a  variable  stored
  9.         somewhere in the address space of the program.  It is always
  10.         best to use an example so load the file named POINTER.C  and
  11.         display  it on your monitor for an example of a program with
  12.         some pointers in it.
  13.  
  14.              For  the moment,  ignore the data declaration statement
  15.         where we define "index" and two other fields beginning  with
  16.         a star.   It is properly called an asterisk, but for reasons
  17.         we  will see later,  let's agree to call it a star.   If you
  18.         observe  the  first statement,  it should be clear  that  we
  19.         assign the value of 39 to the variable "index".   This is no
  20.         surprise,  we  have been doing it for several programs  now.
  21.         The  next  statement  however,  says to assign  to  "pt1"  a
  22.         strange looking value,  namely the variable "index" with  an
  23.         ampersand in front of it.   In this example, pt1 and pt2 are
  24.         pointers,  and  the  variable "index" is a simple  variable.
  25.         Now we have a problem.  We need to learn how to use pointers
  26.         in a program, but to do so requires that first we define the
  27.         means of using the pointers in the program.
  28.  
  29.              The  following two rules will be somewhat confusing  to
  30.         you at first but we need to state the definitions before  we
  31.         can  use  them.   Take your time,  and the whole thing  will
  32.         clear up very quickly.
  33.  
  34.                           TWO VERY IMPORTANT RULES
  35.  
  36.              The  following two rules are very important when  using
  37.         pointers and must be thoroughly understood.
  38.  
  39.         1.  A variable name with an ampersand in front of it defines
  40.             the  address of the variable and therefore points to the
  41.             variable.  You can therefore read line seven as "pt1  is
  42.             assigned the value of the address of index".
  43.  
  44.         2.  A  pointer  with a "star" in front of it refers  to  the
  45.             value of the variable pointed to by the  pointer.   Line
  46.             ten  of the program can be read as "The stored (starred)
  47.             value  to which the pointer "pt1" points is assigned the
  48.             value  13".   Now  you can see why it is  convenient  to
  49.             think of the asterisk as a star,  it sort of sounds like
  50.             the word store.
  51.  
  52.                                   MEMORY AIDS
  53.  
  54.             1. Think of & as an address.
  55.             2. Think of * as a star referring to stored.
  56.  
  57.  
  58.                                   Page 52
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                             Chapter 8 - Pointers
  69.  
  70.  
  71.  
  72.              Assume for the moment that "pt1" and "pt2" are pointers
  73.         (we will see how to define them shortly).  As pointers, they
  74.         do not contain a variable value but an address of a variable
  75.         and  can  be  used to point to a variable.  Line  7  of  the
  76.         program  assigns the pointer "pt1" to point to the  variable
  77.         we have already defined as "index" because we have  assigned
  78.         the address of "index" to "pt1".  Since we have a pointer to
  79.         "index",  we  can manipulate the value of "index"  by  using
  80.         either the variable name itself, or the pointer.
  81.  
  82.              Line 10 modifies the value by using the pointer.  Since
  83.         the  pointer  "pt1"  points to the  variable  "index",  then
  84.         putting  a star in front of the pointer name refers  to  the
  85.         memory location to which it is pointing.  Line 10  therefore
  86.         assigns to "index" the value of 13. Anyplace in the  program
  87.         where it is permissible to use the variable name "index", it
  88.         is  also permissible to use the name "*pt1" since  they  are
  89.         identical in meaning until the pointer is reassigned to some
  90.         other variable.
  91.  
  92.                               ANOTHER POINTER
  93.  
  94.              Just  to add a little intrigue to the system,  we  have
  95.         another  pointer  defined in this  program,  "pt2".    Since
  96.         "pt2" has not been assigned a value prior to statement 8, it
  97.         doesn't point to anything, it contains garbage.  Of  course,
  98.         that is also true of any variable until a value is  assigned
  99.         to it. Statement 8 assigns "pt2" the same address as  "pt1",
  100.         so  that now "pt2" also points to the variable "index".   So
  101.         to continue the definition from the last paragraph, anyplace
  102.         in  the program where it is permissible to use the  variable
  103.         "index",  it  is  also permissible to use  the  name  "*pt2"
  104.         because  they   are  identical in  meaning.   This  fact  is
  105.         illustrated  in  the  first "printf"  statement  since  this
  106.         statement  uses  the  three means of  identifying  the  same
  107.         variable to print out the same variable three times.
  108.  
  109.                          THERE IS ONLY ONE VARIABLE
  110.  
  111.              Note carefully that,  even though it appears that there
  112.         are three variables, there is really only one variable.  The
  113.         two  pointers  point  to  the  single  variable.    This  is
  114.         illustrated in the next statement which assigns the value of
  115.         13  to  the  variable "index",  because that  is  where  the
  116.         pointer  "pt1"  is pointing.   The next  "printf"  statement
  117.         causes  the  new value of 13 to be printed out three  times.
  118.         Keep  in mind that there is really only one variable  to  be
  119.         changed, not three.
  120.  
  121.  
  122.  
  123.  
  124.                                   Page 53
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                             Chapter 8 - Pointers
  135.  
  136.  
  137.              This is admittedly a very difficult concept,  but since
  138.         it  is  used  extensively  in all but  the  most  trivial  C
  139.         programs,  it  is  well  worth your time to stay  with  this
  140.         material until you understand it thoroughly.
  141.  
  142.                         HOW DO YOU DECLARE A POINTER?
  143.  
  144.              Now  to  keep a promise and tell you how to  declare  a
  145.         pointer.   Refer  to the third line of the program  and  you
  146.         will  see  our  old familiar way of  defining  the  variable
  147.         "index",  followed  by  two more  definitions.   The  second
  148.         definition  can  be read as "the storage location  to  which
  149.         "pt1"  points  will be an int  type  variable".   Therefore,
  150.         "pt1" is a pointer to an int type variable.  Likewise, "pt2"
  151.         is another pointer to an int type variable.
  152.  
  153.              A  pointer  must  be defined to point to some  type  of
  154.         variable.   Following a proper definition, it cannot be used
  155.         to point to any other type of variable or it will result  in
  156.         a  "type incompatibility" error.   In the same manner that a
  157.         "float"  type of variable cannot be added to an  "int"  type
  158.         variable,  a pointer to a "float" variable cannot be used to
  159.         point to an integer variable.
  160.  
  161.              Compile and run this program and observe that there  is
  162.         only  one  variable  and the single  statement  in  line  10
  163.         changes the one variable which is displayed three times.
  164.  
  165.                       THE SECOND PROGRAM WITH POINTERS
  166.  
  167.              In these few pages so far on pointers,  we have covered
  168.         a lot of territory, but it is important territory.  We still
  169.         have  a  lot  of  material to cover so stay in  tune  as  we
  170.         continue  this important aspect of C.   Load the  next  file
  171.         named  POINTER2.C  and display it on your monitor so we  can
  172.         continue our study.
  173.  
  174.              In  this program we have defined several variables  and
  175.         two pointers.   The first pointer named "there" is a pointer
  176.         to  a "char" type variable and the second named "pt"  points
  177.         to an "int" type variable.  Notice also that we have defined
  178.         two  array variables named "strg" and "list".   We will  use
  179.         them  to show the correspondence between pointers and  array
  180.         names.
  181.  
  182.                   A STRING VARIABLE IS ACTUALLY A POINTER
  183.  
  184.              In  the programming language C,  a string  variable  is
  185.         defined to be simply a pointer to the beginning of a string.
  186.         This  will  take  some explaining.   Refer  to  the  example
  187.         program  on  your monitor.   You will notice that  first  we
  188.  
  189.  
  190.                                   Page 54
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                             Chapter 8 - Pointers
  201.  
  202.  
  203.         assign a string constant to the string variable named "strg"
  204.         so we will have some data to work with.  Next, we assign the
  205.         value  of the first element to the variable "one",  a simple
  206.         "char" variable.   Next,  since the string name is a pointer
  207.         by  definition  of the C language,  we can assign  the  same
  208.         value to "two" by using the star and the string  name.   The
  209.         result  of  the two assignments are such that "one" now  has
  210.         the same value as "two", and both contain the character "T",
  211.         the  first character in the string.   Note that it would  be
  212.         incorrect  to  write  the ninth line as  "two  =  *strg[0];"
  213.         because the star takes the place of the square brackets.
  214.  
  215.              For all practical purposes,  "strg" is a  pointer.   It
  216.         does, however, have one restriction that a true pointer does
  217.         not  have.   It cannot be changed like a variable,  but must
  218.         always contain the initial value and therefore always points
  219.         to  its  string.   It  could  be thought  of  as  a  pointer
  220.         constant,  and in some applications you may desire a pointer
  221.         that cannot be corrupted in any way.   Even though it cannot
  222.         be changed, it can be used to refer to other values than the
  223.         one  it is defined to point to,  as we will see in the  next
  224.         section of the program.
  225.  
  226.              Moving ahead to line 13, the variable "one" is assigned
  227.         the  value of the ninth variable (since the indexing  starts
  228.         at zero) and "two" is assigned the same value because we are
  229.         allowed to index a pointer to get to values farther ahead in
  230.         the string.  Both variables now contain the character "a".
  231.  
  232.             The C programming language takes care of indexing for us
  233.         automatically  by  adjusting the indexing for  the  type  of
  234.         variable  the  pointer is pointing to.   In this  case,  the
  235.         index  of  8  is simply added to the  pointer  value  before
  236.         looking up the desired result because a "char" type variable
  237.         is  one byte long.   If we were using a pointer to an  "int"
  238.         type variable,  the index would be doubled and added to  the
  239.         pointer  before  looking up the value because an "int"  type
  240.         variable  uses two bytes per value stored.   When we get  to
  241.         the chapter on structures,  we will see that a variable  can
  242.         have  many,   even  into  the  hundreds  or  thousands,   of
  243.         bytes  per  variable,  but  the  indexing  will  be  handled
  244.         automatically for us by the system.
  245.  
  246.              Since "there" is already a pointer, it can be  assigned
  247.         the  address  of  the  eleventh element  of  "strg"  by  the
  248.         statement  in line 17 of the program.  Remember  that  since
  249.         "there"  is a true pointer, it can be assigned any value  as
  250.         long as that value represents a "char" type of address.   It
  251.         should  be clear that the pointers must be "typed" in  order
  252.         to  allow  the  pointer arithmetic  described  in  the  last
  253.  
  254.  
  255.  
  256.                                   Page 55
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                             Chapter 8 - Pointers
  267.  
  268.  
  269.         paragraph to be done properly.  The third and fourth outputs
  270.         will be the same, namely the letter "c".
  271.  
  272.                              POINTER ARITHMETIC
  273.  
  274.              Not  all  forms  of  arithmetic are  permissible  on  a
  275.         pointer.   Only  those things that make  sense,  considering
  276.         that a pointer is an address somewhere in the computer.   It
  277.         would  make sense to add a constant to an  address,  thereby
  278.         moving it ahead in memory that number of places.   Likewise,
  279.         subtraction  is permissible,  moving it back some number  of
  280.         locations.   Adding  two  pointers together would  not  make
  281.         sense  because absolute memory addresses are  not  additive.
  282.         Pointer multiplication is also not allowed, as that would be
  283.         a  funny number.   If you think about what you are  actually
  284.         doing,  it will make sense to you what is allowed,  and what
  285.         is not.
  286.  
  287.                          NOW FOR AN INTEGER POINTER
  288.  
  289.              The  array named "list" is assigned a series of  values
  290.         from  100  to 199 in order to have some data to  work  with.
  291.         Next  we  assign the pointer "pt" the address  of  the  28th
  292.         element  of the list and print out the same value both  ways
  293.         to  illustrate that the system truly will adjust  the  index
  294.         for the "int" type variable.  You should spend some time  in
  295.         this program until you feel you fairly well understand these
  296.         lessons on pointers.
  297.  
  298.              Compile and run POINTER2.C and study the output.
  299.  
  300.              You  may recall that back in the lesson on functions we
  301.         mentioned that there were two ways to get variable data back
  302.         from a function.   One way is through use of the array,  and
  303.         you should be right on the verge of guessing the other  way.
  304.         If your guess is through use of a pointer,  you are correct.
  305.         Load  and display the program named TWOWAY.C for an  example
  306.         of this.
  307.  
  308.                     FUNCTION DATA RETURN WITH A POINTER
  309.  
  310.              In  TWOWAY.C,  there  are two variables defined in  the
  311.         main program "pecans" and "apples".   Notice that neither of
  312.         these is defined as a pointer.   We assign values to both of
  313.         these  and print them out,  then call the  function  "fixup"
  314.         taking with us both of these values.   The variable "pecans"
  315.         is  simply  sent  to the function,  but the address  of  the
  316.         variable  "apples" is sent to the function.   Now we have  a
  317.         problem.   The two arguments are not the same, the second is
  318.         a pointer to a variable.  We must somehow alert the function
  319.         to  the  fact  that it is supposed  to  receive  an  integer
  320.  
  321.  
  322.                                   Page 56
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.                             Chapter 8 - Pointers
  333.  
  334.  
  335.         variable  and a pointer to an integer variable.   This turns
  336.         out   to  be  very  simple.    Notice  that  the   parameter
  337.         definitions in the function define "nuts" as an integer, and
  338.         "fruit"  as a pointer to an integer.   The call in the  main
  339.         program  therefore  is now in agreement  with  the  function
  340.         heading and the program interface will work just fine.
  341.  
  342.              In  the body of the function,  we print the two  values
  343.         sent  to  the function,  then modify them and print the  new
  344.         values out.   This should be perfectly clear to you by  now.
  345.         The  surprise occurs when we return to the main program  and
  346.         print out the two values again.  We will find that the value
  347.         of  pecans will be restored to its value before the function
  348.         call  because  the C language makes a copy of  the  item  in
  349.         question and takes the copy to the called function,  leaving
  350.         the original intact.   In the case of the variable "apples",
  351.         we  made  a copy of a pointer to the variable and  took  the
  352.         copy of the pointer to the function.  Since we had a pointer
  353.         to  the  original variable,  even though the pointer  was  a
  354.         copy,  we  had  access  to the original variable  and  could
  355.         change  it in the function.   When we returned to  the  main
  356.         program,  we  found  a  changed value in  "apples"  when  we
  357.         printed it out.
  358.  
  359.              By  using  a pointer in a function call,  we  can  have
  360.         access  to the data in the function and change it in such  a
  361.         way  that when we return to the calling program,  we have  a
  362.         changed  value  of data.    It must be pointed out  however,
  363.         that  if you modify the value of the pointer itself  in  the
  364.         function,  you  will have a restored pointer when you return
  365.         because the pointer you use in the function is a copy of the
  366.         original.  In this example, there was no pointer in the main
  367.         program because we simply sent the address to the  function,
  368.         but  in  many  programs you will use  pointers  in  function
  369.         calls.  One of the places you will find need for pointers in
  370.         function  calls  will be when you request data  input  using
  371.         standard  input/output routines.   These will be covered  in
  372.         the next two chapters.
  373.  
  374.              Compile and run TWOWAY.C and observe the output.
  375.  
  376.                            POINTERS ARE VALUABLE
  377.  
  378.              Even  though  you are probably somewhat intimidated  at
  379.         this point by the use of pointers,  you will find that after
  380.         you  gain experience,  you will use them profusely  in  many
  381.         ways.  You will also use pointers in every program you write
  382.         other than the most trivial because they are so useful.  You
  383.         should  probably  go  over this material  carefully  several
  384.         times until you feel comfortable with it because it is  very
  385.  
  386.  
  387.  
  388.                                   Page 57
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.                             Chapter 8 - Pointers
  399.  
  400.  
  401.         important  in the area of input/output which is next on  the
  402.         agenda.
  403.  
  404.  
  405.         PROGRAMMING EXERCISES
  406.  
  407.         1.   Define  a character array  and use "strcpy" to  copy  a
  408.              string  into it.  Print the string out by using a  loop
  409.              with  a  pointer to print out one character at a  time.
  410.              Initialize the pointer to the first element and use the
  411.              double  plus  sign  to increment  the  pointer.  Use  a
  412.              separate  integer variable to count the  characters  to
  413.              print.
  414.  
  415.         2.   Modify the program to print out the string backwards by
  416.              pointing to the end and using a decrementing pointer.
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.                                   Page 58
  455.